﻿/*******************************************************************************
 * Copyright © 2020 WaterCloud.Framework 版权所有
 * Author: WaterCloud
 * Description: WaterCloud快速开发平台
 * Website：
*********************************************************************************/

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.Domain.SystemOrganize;
using WaterCloud.Service.SystemOrganize;

namespace WaterCloud.Web.Areas.SystemOrganize.Controllers
{
	[Area("SystemOrganize")]
	public class OrganizeController : BaseController
	{
		public OrganizeService _service { get; set; }

		[HttpGet]
		public virtual ActionResult AddForm()
		{
			return View();
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetTreeSelectJson()
		{
			var data = await _service.GetList();
			var treeList = new List<TreeSelectModel>();
			foreach (OrganizeEntity item in data)
			{
				TreeSelectModel treeModel = new TreeSelectModel();
				treeModel.id = item.F_Id;
				treeModel.text = item.F_FullName;
				treeModel.parentId = item.F_ParentId;
				treeModel.data = item;
				treeList.Add(treeModel);
			}
			return Content(treeList.TreeSelectJson());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetSelectJson()
		{
			var data = await _service.GetList();
			data = data.Where(a => a.F_EnabledMark == true).ToList();
			var treeList = new List<TreeSelectModel>();
			foreach (OrganizeEntity item in data)
			{
				TreeSelectModel treeModel = new TreeSelectModel();
				treeModel.id = item.F_Id;
				treeModel.text = item.F_FullName;
				treeModel.parentId = item.F_ParentId;
				treeModel.data = item;
				treeList.Add(treeModel);
			}
			return Content(treeList.TreeSelectJson());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetTreeJson()
		{
			var data = await _service.GetList();
			var treeList = new List<TreeViewModel>();
			foreach (OrganizeEntity item in data)
			{
				TreeViewModel tree = new TreeViewModel();
				bool hasChildren = data.Count(t => t.F_ParentId == item.F_Id) == 0 ? false : true;
				tree.id = item.F_Id;
				tree.text = item.F_FullName;
				tree.value = item.F_EnCode;
				tree.parentId = item.F_ParentId;
				tree.isexpand = true;
				tree.complete = true;
				tree.hasChildren = hasChildren;
				treeList.Add(tree);
			}
			return Content(treeList.TreeViewJson());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetTreeGridJson(string keyword, string ids)
		{
			var data = await _service.GetLookList();
			if (!string.IsNullOrEmpty(keyword))
			{
				data = data.TreeWhere(t => t.F_FullName.Contains(keyword));
			}
			if (!string.IsNullOrEmpty(ids))
			{
				var str = ids.Split(',');
				foreach (var item in str)
				{
					if (data.Where(a => a.F_Id == item).Any())
					{
						var temp = data.Find(a => a.F_Id == item);
						temp.LAY_CHECKED = true;
					}
				}
			}
			return Success(data.Count, data);
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetFormJson(string keyValue)
		{
			var data = await _service.GetLookForm(keyValue);
			return Content(data.ToJson());
		}

		[HttpPost]
		[HandlerAjaxOnly]
		public async Task<ActionResult> SubmitForm(OrganizeEntity organizeEntity, string keyValue)
		{
			try
			{
				if (organizeEntity.F_ParentId == "0")
				{
					organizeEntity.F_Layers = 1;
				}
				else
				{
					organizeEntity.F_Layers = (await _service.GetForm(organizeEntity.F_ParentId)).F_Layers + 1;
				}
				await _service.SubmitForm(organizeEntity, keyValue);
				return await Success("操作成功。", "", keyValue);
			}
			catch (Exception ex)
			{
				return await Error(ex.Message, "", keyValue);
			}
		}

		[HttpPost]
		[HandlerAjaxOnly]
		[HandlerAuthorize]
		public async Task<ActionResult> DeleteForm(string keyValue)
		{
			try
			{
				await _service.DeleteForm(keyValue);
				return await Success("操作成功。", "", keyValue, DbLogType.Delete);
			}
			catch (Exception ex)
			{
				return await Error(ex.Message, "", keyValue, DbLogType.Delete);
			}
		}
	}
}